package com.meitianhui.convenitenbuy.module.mine.adapter;

import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.model.EaseAtMessageHelper;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseArrayResponse;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.HyphenateInfo;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.GroupBean;
import com.meitianhui.convenitenbuy.module.mine.model.HXUserBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.MessageDisPitchActivity;
import com.meitianhui.convenitenbuy.util.DateUtil;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by amy on 2017/11/23.
 */

public class MessageDispitchAdapter extends BaseQuickAdapter<EMConversation,BaseViewHolder> {

    private MessageDisPitchActivity context;

    private String mTitles[] = { Constants.HYPHENATE_ID_HOME_SERVICE,
            Constants.HYPHENATE_ID_FIGHT_GROUP, Constants.HYPHENATE_ID_EDINBURGH, Constants.HYPHENATE_ID_BRAND,Constants.HYPHENATE_ID_STORE};



    public MessageDispitchAdapter(@Nullable List<EMConversation> data, MessageDisPitchActivity activity) {
        super(R.layout.ease_row_chat_history, data);
        this.context = activity;
       // getAllGroupmsg();
    }

    @Override
    protected void convert(BaseViewHolder helper, EMConversation conversation) {
        String groupId = conversation.conversationId();
        String from = conversation.getLastMessage().getFrom();
        String to = conversation.getLastMessage().getTo();
        //通过userNameId 去获取用户的nickName不然会显示一串id

        if (conversation.getType() == EMConversation.EMConversationType.GroupChat){
            //如果是群组聊天

            if(EaseAtMessageHelper.get().hasAtMeMsg(groupId)){
                helper.setVisible(R.id.mentioned,true);
            }else{
                helper.setVisible(R.id.mentioned,false);
            }
            // group message, show group avatar
            helper.setImageResource(R.id.avatar,R.drawable.ease_group_icon);
            EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
           //获取到所有群组信息
            helper.setText(R.id.name,group != null ? group.getGroupName() : "");

        }else if(conversation.getType() == EMConversation.EMConversationType.ChatRoom){
            //直播的吧

        }else {

            for (int i = 0; i <mTitles.length ; i++) {
                if (to.equals(mTitles[i])) {
                    helper.setText(R.id.name, "官方客服");
                    helper.getView(R.id.mentioned).setVisibility(View.GONE);
                } else {
                    //单聊
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("im_user_id", to);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    MineApiDataSource.getInstance().getMsgSingle(InfoProvider.getUserToken(mContext).getUser_token(), "chat.getIMUserDetail", jsonObject.toString()).subscribe(new ResponseObserver<BaseResponse<HXUserBean>>(context) {
                        @Override
                        public void onSuccess(BaseResponse<HXUserBean> hxUserBeanResponse) {
                            helper.setText(R.id.name, hxUserBeanResponse.data.getNickname().equals("") ? "匿名用户" : hxUserBeanResponse.data.getNickname());
                        }
                    });
                    helper.getView(R.id.mentioned).setVisibility(View.GONE);
                }

            }

        }

        if (conversation.getUnreadMsgCount() > 0) {
            // show unread message count
            helper.setText(R.id.unread_msg_number,String.valueOf(conversation.getUnreadMsgCount()));
            helper.setVisible(R.id.unread_msg_number,true);

        } else {
            helper.setVisible(R.id.unread_msg_number,false);
        }

        if (conversation.getAllMsgCount() != 0) {
            // show the content of latest message
            EMMessage lastMessage = conversation.getLastMessage();
            helper.setText(R.id.message, EaseSmileUtils.getSmiledText(context, EaseCommonUtils.getMessageDigest(lastMessage,context).trim()));
            helper.setText(R.id.time, DateUtil.getTimestampString(new Date(lastMessage.getMsgTime())));
            if (lastMessage.direct() == EMMessage.Direct.SEND && lastMessage.status() == EMMessage.Status.FAIL) {
                helper.getView(R.id.msg_state).setVisibility(View.VISIBLE);
              //  helper.setVisible(R.id.msg_state,true);
            } else {
                helper.getView(R.id.msg_state).setVisibility(View.GONE);
            }
        }



    }



    ArrayList<GroupBean> MsgGroupList = new ArrayList<>();
    private void getAllGroupmsg() {

        JSONObject jsonObject = new JSONObject();
        try{
            HyphenateInfo hyphenateInfo= (HyphenateInfo) PreferenceUtils.readObject(mContext, Constants.HYPHENATE_INFO);
            jsonObject.put("im_user_id",hyphenateInfo.getIm_user_id());
        }catch (Exception e){
            e.printStackTrace();
        }
        MineApiDataSource.getInstance().getMsgGroup(InfoProvider.getUserToken(mContext).getUser_token(),"chat.getIMUserGroupList",jsonObject.toString()).subscribe(new ResponseObserver<BaseArrayResponse<GroupBean>>(context) {
            @Override
            public void onSuccess(BaseArrayResponse<GroupBean> s) {
                MsgGroupList = s.data;

            }

            @Override
            public void onFail(int code, String msg) {
                super.onFail(code, msg);
            }
        });
    }
}
